package com.situ.shoplook.admin.api;

import com.github.pagehelper.PageInfo;
import com.situ.shoplook.admin.model.Admin;
import com.situ.shoplook.admin.service.AdminService;
import com.situ.shoplook.common.model.Category;
import com.situ.shoplook.common.model.Result;
import com.situ.shoplook.common.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v1/admin", produces = "application/json;charset=utf-8")
@Slf4j
public class AdminApi {

    private AdminService adminService;

    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    /**
     * add
     */
    @PostMapping
    public Result add(@RequestBody Admin admin) {
        try {
            log.info(admin.toString());
            adminService.save(admin);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * remove
     */
    @DeleteMapping
    public Result remove(@RequestBody Integer[] ids) {
        log.info(Arrays.toString(ids));
            adminService.remove(ids);
            return Result.success();
    }
    /**
     * update
     */
    @PutMapping
    public Result update(@RequestBody Admin admin) {
        try {
            adminService.update(admin);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * findAll
     */
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "1",value = "page") Integer page,
                          @RequestParam(defaultValue = "10",value = "limit") Integer limit, Admin admin) {
        try {
            PageInfo<Admin> pageInfos = adminService.searchByPage(page, limit, admin);
            return Result.success(pageInfos);
        }catch (Exception e){
            return Result.error("无查询结果");
        }

    }

    /**
     * update
     */
    @PutMapping("/state")
    public Result updataByState(@RequestBody Admin admin) {
        try {
            if(admin.getLocked() == 0){
                admin.setLocked(1);
            }else{
                admin.setLocked(0);
            }
            adminService.update(admin);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
